package com.example.epidemic_assistant.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.epidemic_assistant.gen.DaoSession;
import com.android.epidemic_assistant.gen.UserBeanDao;
import com.example.epidemic_assistant.App;
import com.example.epidemic_assistant.BaseActivity;
import com.example.epidemic_assistant.R;
import com.example.epidemic_assistant.bean.UserBean;
import com.example.epidemic_assistant.ui.adapter.PlaceAdapter;

import java.util.ArrayList;
import java.util.List;

public class PlacesVisitedActivity extends BaseActivity implements PlaceAdapter.IDeleteClick {

    private final static String TAG = "PlacesVisitedActivity";

    private RecyclerView mRvPlace;

    private PlaceAdapter mPlaceAdapter;

    private List<String> mPlaces;
    private ArrayList<String> mPlaceList = new ArrayList<>();

    private String mUsername;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    private App mMyApp;
    private DaoSession mDaoSession;
    private UserBeanDao mUserBeanDao;
    private UserBean mUserBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places_visited);

        mRvPlace = findViewById(R.id.rv_places_visited);

        mSharedPreferences = this.getSharedPreferences("autoLogin", MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
        String username = mSharedPreferences.getString("username", "");
        if (!username.isEmpty()) {
            mUsername = username;
        } else {
            mUsername = this.getIntent().getExtras().getString("username");
        }
        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mUserBeanDao = mDaoSession.getUserBeanDao();
        List<UserBean> userBeanList = mUserBeanDao.queryBuilder().where(UserBeanDao.Properties.UserName.eq(mUsername)).list();
        for (int i = 0; i < userBeanList.size(); i++) {
            UserBean userBean = userBeanList.get(i);
            if (userBean.getUserName().equals(username)) {
                mUserBean = userBean;
            }
        }
        if (mUserBean != null) {
            if (mUserBean.getPlaces() != null) {
                mPlaces = mUserBean.getPlaces();
                mPlaceList.addAll(mPlaces);
            }
        }
        Log.d(TAG,"mPlaceList-->"+mPlaceList);
        mRvPlace.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mPlaceAdapter = new PlaceAdapter(this, mPlaceList);
        mPlaceAdapter.setOnDeleteClick(this);
        mRvPlace.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRvPlace.setAdapter(mPlaceAdapter);
    }

    @Override
    public void onDeleteClick(int position) {
        //去过的地方删除
        mPlaceList.remove(position);
        mPlaceAdapter.notifyDataSetChanged();
        mUserBean.setPlaces(mPlaceList);
        mUserBeanDao.update(mUserBean);
    }
}